var nowPoint = null
var map = null
var marker1 = null
const PHONE_REG = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
const ID_CARD_REG = /^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
const ID_CARD_GAT_REG = /^(8[123]0000)(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
var vue = new Vue({
    el: "#main",
    data() {
        return {
            loading: false, //点击完成的loading
            msg: '',
            prompt: false,
            dateShow: false, //
            times: [{
                label: '入户探访1次',
                value: 1
            }, {
                label: '电话探访1次',
                value: 2
            }, {
                label: '入户+电话探访1次',
                value: 3
            }, ], //频率
            idCardType: [{
                label: '内地居民身份证',
                value: 'jm'
            }, {
                label: '护照',
                value: 'hz'
            }, {
                label: '港珠澳居住证',
                value: 'gza'
            }],
            categoryList: [{
                value: '1类',
            }, {
                value: '2类',
            }, {
                value: '3类',
            }, {
                value: '4类',
            }, {
                value: '5类',
            }, ], //类别
            // 地区配置项
            areaList: areaList,
            areaShow: false,
            areaValue: '',
            areaValueTypt: 0, //0代表户籍地区    1代表居住地区

            ruleForm: {
                dept_id: '', //网格id
                protector_user_id: '', //负责人id
                frequency: '', //频率
                realname: "", //姓名
                id_cardType: '', //证件类型
                id_card: '', //身份证号码
                category: '', //类别 (四类 五类 等)
                sick_type: '', //具体分类

                is_government_services: '', //是否享受政府购买服务（是/否）
                is_transform: '', //是否进行过适老化改造（是/否）
                is_voice_call: '', //是否安装语音呼叫器等智能设备（是/否）

                phone: '', //本人或子女联系人电话
                _birthplace: '', //户籍地区
                birthplace_province_id: "", //户籍地城市 id
                birthplace_city_id: "", //户籍地地区 id
                birthplace_area_id: "", //户籍地地址 id
                birthplace_street: '', //户籍详细地址

                SickManContacts: [{
                    realname: '', //姓名
                    relationship: '', //关系
                    phone: '', //手机号
                }, {
                    realname: '', //姓名
                    relationship: '', //关系
                    phone: '', //手机号
                }, {
                    realname: '', //姓名
                    relationship: '', //关系
                    phone: '', //手机号
                }], //紧急联系人1、2、3

                _address: '', //居住地区
                province_id: "", //居住地城市 id
                city_id: "", //居住地地区 id
                area_id: "", //居住地地址 id
                street: '', //居住详细地址

                caring_measures: '', //关爱措施
                remark: '', //备注
            },
            rules: {
                realname: [{
                    required: true,
                    message: '请输入姓名'
                }],
            },
            location: '',
        }
    },
    created() {
        this.ruleForm.dept_id = getRequest().id
        this.ruleForm.protector_user_id = getRequest().user_id
    },
    mounted() {

    },
    methods: {
        // 新增
        submit() {
            this.loading = true
            if (this.ruleForm.frequency == "") {
                this.prompt = true
                this.loading = false
                this.msg = '请选择频率'
                return
            } else if (this.ruleForm.realname == "") {
                this.prompt = true
                this.loading = false
                this.msg = '请输入姓名'
                return
            } else if (this.ruleForm.id_card) {
                if (!(ID_CARD_REG.test(this.ruleForm.id_card)) && !(ID_CARD_GAT_REG.test(this.ruleForm.id_card)) && (this.idCardType == 'jm' || this.idCardType == 'gza')) {
                    this.prompt = true
                    this.loading = false
                    this.msg = '身份证号码格式不正确'
                    return
                }
            } else if (this.ruleForm.phone) {
                if (!(PHONE_REG.test(this.ruleForm.phone))) {
                    this.prompt = true
                    this.loading = false
                    this.msg = '本人或子女联系人电话格式不正确'
                    return
                }
            } else if (this.ruleForm.SickManContacts[0].phone) {
                if (!(PHONE_REG.test(this.ruleForm.SickManContacts[0].phone))) {
                    this.prompt = true
                    this.loading = false
                    this.msg = '紧急联系人1电话格式不正确'
                    return
                }
            } else if (this.ruleForm.SickManContacts[1].phone) {
                if (!(PHONE_REG.test(this.ruleForm.SickManContacts[1].phone))) {
                    this.prompt = true
                    this.loading = false
                    this.msg = '紧急联系人2电话格式不正确'
                    return
                }
            } else if (this.ruleForm.SickManContacts[2].phone) {
                if (!(PHONE_REG.test(this.ruleForm.SickManContacts[2].phone))) {
                    this.prompt = true
                    this.loading = false
                    this.msg = '紧急联系人3电话格式不正确'
                    return
                }
            }
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    ajax({
                        url: formal + '/sick-man',
                        type: "POST",
                        data: this.ruleForm,
                        success: (res) => {
                            this.msg = '新增成功'
                            this.prompt = true
                            this.loading = false
                        },
                        error: (err) => {
                            this.msg = err.message || err.responseJSON[0].message
                            this.prompt = true
                            this.loading = false
                        }
                    })
                } else {
                    this.loading = false
                }
            });

        },
        // 点击弹窗确认
        goPlaceMan() {
            if (this.msg == "新增成功") {
                this.prompt = false
                history.back()
            } else {
                this.prompt = false
            }
            this.loading = false
        },
        // 时间选择器确定事件
        dateTimeConfirm(e) {
            let time = new Date(e)
            let t = time.getTime()
            let year = time.getFullYear()
            let mon = time.getMonth() + 1
            mon = mon > 10 ? mon : '0' + mon
            let day = time.getDate()
            day = day > 10 ? day : '0' + day
            let str = year + '-' + mon + '-' + day
            this.ruleForm._birthday = str
            this.ruleForm.birthday = t / 1000
            this.dateShow = false
        },
        // 展示地区选择器
        showArea(val) {
            this.areaValueTypt = val
            this.areaShow = true
        },
        // 地区选择弹窗
        areaConfirm(area) {

            if (this.areaValueTypt == 0) {
                this.ruleForm._birthplace = area.map((option) => option.name).join('/');
                this.ruleForm.birthplace_province_id = area[0].code
                this.ruleForm.birthplace_city_id = area[1].code
                this.ruleForm.birthplace_area_id = area[2].code
            } else if (this.areaValueTypt == 1) {
                this.ruleForm._address = area.map((option) => option.name).join('/');
                this.ruleForm.province_id = area[0].code
                this.ruleForm.city_id = area[1].code
                this.ruleForm.area_id = area[2].code
            }
            this.areaShow = false
        },
    },
})